//
//  SelectChineseView.m
//  GC_OC
//
//  Created by 曾 on 2022/1/17.
//

#import "SelectChineseView.h"
#import "SelectTableViewCell.h"
@interface SelectChineseView()<UITableViewDelegate,UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, strong)NSMutableArray* dataSource;
@property (nonatomic, strong)UITableView* tableview;
@property (nonatomic, assign)NSInteger selectIndex;
@property (nonatomic, strong)ObjectAFNetWorking* objectAFN;
@property (nonatomic, weak)id<SelectChineseViewDelegate>myDelegate;
@property (nonatomic, strong) NSString *cacheId;
@end

@implementation SelectChineseView

- (instancetype)initWithIdStr:(NSString*)idStr andDelegate:(id<SelectChineseViewDelegate>)delegate{
    self = [super initWithFrame:CGRectMake(0, 0, 0, 0)];
    if (self) {
        self.myDelegate = delegate;
        self.idString = idStr;
        self.selectIndex = 0;
        self.objectAFN=[[ObjectAFNetWorking alloc]init];
        self.dataSource=[NSMutableArray new];
        [self initView];
    }
    return self;
}

-(void)initView
{
    _tableview=[[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableview.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.emptyDataSetDelegate = self;
    _tableview.emptyDataSetSource = self;
    _tableview.showsVerticalScrollIndicator = NO;
    [_tableview registerNib:[UINib nibWithNibName:@"SelectTableViewCell" bundle:nil] forCellReuseIdentifier:@"SelectTableViewCell"];
    _tableview.backgroundColor=RBG(245,245,245);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 60)];
    [self addSubview:_tableview];
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.tableview reloadData];
    [self getData];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithRed:0.84 green:0.84 blue:0.84 alpha:1.00];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self);
        make.width.equalTo(@(1));
    }];
}

-(void)upDateIDStr:(NSString*)idString
{
    if([self.idString integerValue]!=[idString integerValue])
    {
        self.idString=idString;
        self.selectIndex=0;
        [self.dataSource removeAllObjects];
        [self.tableview reloadData];
        [self.myDelegate upDateChineseUIWithTitle:@""];
        [self getData];
    }
}

-(void)getData
{
    @weakify(self)
    [FKCacheTool getIndexData:self.idString complete:^(id data) {
        @strongify(self)
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"media_class"]];
        if (self.dataSource.count != 0) {
            [self.myDelegate upDateSelectChineseDown:self.dataSource[0][@"id"]];
        }
        [self.tableview reloadData];
        if ([self.tableview numberOfRowsInSection:0]) {
            NSIndexPath *indexPathOne = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableview scrollToRowAtIndexPath:indexPathOne atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
        
        [self.myDelegate upDateChineseUIWithTitle:self.dataSource.count>0 ? self.dataSource[0][@"name"]:@""];
    }];
}
#pragma mark - tableview delegate and datasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return wid(90);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"SelectTableViewCell"];
    cell.contentInfo = self.dataSource[indexPath.row][@"name"];
    cell.isSelected = (indexPath.row==self.selectIndex);
    return cell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    self.selectIndex=indexPath.row;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tableview reloadData];
    [self.myDelegate selectChineseViewIDStr:self.dataSource[indexPath.row][@"id"]];
    [self.myDelegate upDateChineseUIWithTitle:self.dataSource[indexPath.row][@"name"]];
}

-(void)upDateUI
{
    self.tableview.frame=self.bounds;
    [self.tableview reloadData];
}

- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
}

- (BOOL)hasNextAndUpdateNext {
    self.selectIndex += 1;
    if (self.selectIndex < self.dataSource.count) {
        [self.myDelegate selectChineseViewIDStr:self.dataSource[self.selectIndex][@"id"]];
        [self.myDelegate upDateChineseUIWithTitle:self.dataSource[self.selectIndex][@"name"]];
        [self.tableview reloadData];
        return true;
    } else {
        return false;
    }
}

- (void)saveIdStr {
//    if (self.selectIndex < self.dataSource.count &&  self.dataSource[self.selectIndex][@"id"]) {
//        [[NSUserDefaults standardUserDefaults] setObject:self.dataSource[self.selectIndex][@"id"] forKey:@"SelectChineseView"];
//    }
}

- (void)getIdStr {
//    self.cacheId = [[NSUserDefaults standardUserDefaults] objectForKey:@"SelectChineseView"];
//    if (self.dataSource.count > 0) {
//        [self.tableview reloadData];
//        [self.dataSource enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            if([self.cacheId isEqual:obj[@"id"]]) {
//                self.cacheId = nil;
//                NSIndexPath *indexPathOne = [NSIndexPath indexPathForRow:idx inSection:0];
//                [self.tableview scrollToRowAtIndexPath:indexPathOne atScrollPosition:UITableViewScrollPositionTop animated:YES];
//                *stop = true;
//            }
//        }];
//    }
}

#pragma mark DZNEmptyDataSetDelegate & DZNEmptyDataSetSource
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无内容";
    NSDictionary *attributes = @{
                                 NSFontAttributeName: [UIFont systemFontOfSize:15 weight:UIFontWeightRegular],
                                 NSForegroundColorAttributeName: [UIColor grayColor]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}




@end
